<?php
function bend_field(&$retparams){
	global $_db;
	
	if(!isset($_POST['op'])){
		return BEND_REDIRECT;
	}
	elseif("addview"==$_POST['op']){
		echo rendertpl("applet/fieldaddmod.applet");
		return BEND_RENDERED;
	}
	elseif("add"==$_POST['op']){
		$id = $_db->insert("field", array("bc"=>$_POST['bc'], "field"=>$_POST['field'], "desc"=>$_POST['desc']));
		if($id>0){
			$retparams['errorcode'] = 0;
		}
		else{
			$retparams['errorcode'] = -1;
		}
		return BEND_ECHORET;
	}
	elseif("del"==$_POST['op']){
		$count = $_db->delete("field", "id=$_POST[id]");
	}
	elseif("modview"==$_POST['op']){
		echo rendertpl("applet/fieldaddmod.applet");
		return BEND_RENDERED;
	}
	elseif("mod"==$_POST['op']){
		$count = $_db->update("field", array("bc"=>$_POST['bc'], "field"=>$_POST['field'], "desc"=>$_POST['desc']),"id=$_POST[id]");
		if($count>0){
			$retparams['errorcode'] = 0;
		}
		else{
			$retparams['errorcode'] = -1;
		}
		return BEND_ECHORET;
	}
	elseif("autoupdatefield" == $_POST['op']){
		$_db->delete("field", null, TRUE);
		$tables = $_db->getalltables();
		foreach($tables as $table){
			$fields = $_db->getallfields($table);
			foreach($fields as $field){
				if(null==$_db->select("field", array('id'), "bc='$table' and field='$field'")){
					$_db->insert("field", array("bc"=>substr($table, 2), "field"=>substr($field, 2)));
				}
			}
		}
		//echo "自动更新字段成功!<a href='?view=field'>跳转到管理-字段界面</a>";
		//return true;
	}
	elseif("search"==$_POST['op']){
		return BEND_HOMEPAGE;
	}
	return BEND_REDIRECT;
}
?>